package com.dance.java8.day01;

import com.dance.java8.entity.Person;
import org.junit.Test;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class TestMethodRef {

    @Test
    public void test1(){
        Consumer<String> consumer1 = s -> System.out.println(s);
        // 优化 对象::实例方法名
        // 注意:需要函数式接口方法的入参和返回值 == 引用的对象方法的入参和返回值
        // 1:这个是多写了一行,防止直接看不懂,下面才是简化的
        PrintStream out = System.out;
        Consumer<String> consumer2 =out::println;
        // 简化
        Consumer<String> consumer3 = System.out::println;
        consumer1.accept("hello world");
        consumer2.accept("hello world");
        consumer3.accept("hello world");
    }

    @Test
    public void test2(){
        Comparator<Integer> comparator = Integer::compare;
    }

    @Test
    public void test3(){
        Person person = new Person();
        person.setName("flower");
        Supplier<String> supplier = person::getName;
        System.out.println(supplier.get());
    }

    @Test
    public void test4(){
        BiPredicate<String,String> biPredicate1 = (x,y) -> x.equals(y);
        // 优化 类::实例方法名
        BiPredicate<String,String> biPredicate2 = String::equals;
    }

    @Test
    public void test5(){
        Supplier<Person> supplier1 = () -> new Person();
        // 构造器引用优化
        Supplier<Person> supplier2 = Person::new;
    }

}
